package com.dc.bobaoge.project.v2.to.v3.business.core;

import com.dc.bobaoge.project.base.core.constant.AuthInfoConstant;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreCerCompanyMapper;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreCerMessageMapper;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreCerPersonMapper;
import com.dc.bobaoge.project.v2.to.v3.business.core.util.CerCompanySyncUtil;
import com.dc.bobaoge.project.v2.to.v3.business.core.util.CerPersonSyncUtil;
import com.own.business.sync.trigger.base.entity.SyncTrigger;
import com.own.business.sync.trigger.base.method.ResolveMethod;
import com.own.component.common.exception.BusinessSimpleException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * CerMessageResolveMethod
 *
 * @author chenxueli
 * @date 2024-01-27 13:45:42
 */
@Slf4j
@Component
public class CerMessageResolveMethod implements ResolveMethod {

    @Resource
    private OldCoreCerMessageMapper oldCoreCerMessageMapper;
    @Resource
    private OldCoreCerPersonMapper oldCoreCerPersonMapper;
    @Resource
    private OldCoreCerCompanyMapper oldCoreCerCompanyMapper;

    @Resource
    private CerPersonSyncUtil cerPersonSyncUtil;
    @Resource
    private CerCompanySyncUtil cerCompanySyncUtil;

    /**
     * 修改的处理方法
     *
     * @param trigger 日志信息
     */
    @Override
    public void update(SyncTrigger trigger) {
        if (trigger.getNewStatus() != null) {
            // 判断是不是终态
            if (AuthInfoConstant.OldStatus.PASS != trigger.getNewStatus()) {
                log.debug("不是通过状态，不处理:{},{}", trigger.getNewStatus(), trigger.getTargetId());
                return;
            }
        }
        var oldCoreCerMessage = oldCoreCerMessageMapper.selectById(trigger.getTargetId());
        if (oldCoreCerMessage == null) {
            throw new BusinessSimpleException("认证信息不存在");
        }
        // 如果不是通过状态，不处理
        if (oldCoreCerMessage.getCerMsgState() != AuthInfoConstant.OldStatus.PASS) {
            log.debug("不是通过状态，不处理:{},{}", oldCoreCerMessage.getCerMsgState(), trigger.getTargetId());
            return;
        }
        // 查询实名认证的message信息
        // 查询实名认证的person/company信息
        // 查询押金信息
        if (oldCoreCerMessage.getCerType() == 1) {
            var oldCoreCerPerson = oldCoreCerPersonMapper.selectById(oldCoreCerMessage.getCerSearchId());
            if (oldCoreCerPerson == null) {
                throw new BusinessSimpleException("认证信息不存在");
            }
            cerPersonSyncUtil.runInfoPass(trigger, oldCoreCerMessage, oldCoreCerPerson);
        }
        if (oldCoreCerMessage.getCerType() == 2) {
            var oldCoreCerCompany = oldCoreCerCompanyMapper.selectById(oldCoreCerMessage.getCerSearchId());
            if (oldCoreCerCompany == null) {
                throw new BusinessSimpleException("认证信息不存在");
            }
            cerCompanySyncUtil.runInfoPass(trigger, oldCoreCerMessage, oldCoreCerCompany);
        }
    }

    /**
     * 名称信息
     *
     * @return 名称
     */
    @Override
    public String name() {
        return "cer_message";
    }

}
